package tankgame03_manybullet;

import java.util.Random;

public class EnemyTank extends Tank implements Runnable{
    private long lastShootTime = 0;
    private static final long SHOOT_INTERVAL = 2000; // 毫秒
    Random random =new Random();
    public EnemyTank(int x, int y) {
        super(x, y);

    }

    @Override
    public void run() {
        while (isIslive()){
            if (random.nextInt(100)>85){
                setDirct(random.nextInt(100)%4);
            } else {
                switch (getDirct()) {
                    case 0:
                        moveUp();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case 2:
                        moveDown();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case 1:
                        moveRight();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case 3:
                        moveLeft();
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                }
            }




            if (System.currentTimeMillis() - lastShootTime > SHOOT_INTERVAL) {
                shoot();
                lastShootTime = System.currentTimeMillis();
            }
        }
    }
}
